package com.cherish.web.user.controller;

import com.cherish.redis.RedisService;
import com.cherish.utils.ResultVo;
import com.cherish.web.user.entity.User;
import com.cherish.web.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:MQ
 * Date:2023/12/2317:30
 */
@ApiOperation(value="用户操作User")
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping("/getAllUser")
    public ResultVo getAllUser(){
        List<User> list = userService.list();
        List<User> listView = new ArrayList<>();
        int size = list.size();
        User user = null;
        for(int i = 0;i < size;i++){
            user = list.get(i);
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            listView.add(user);
        }
        return new ResultVo("",200,listView);
    }

    @RequestMapping("/getPassword")
    public ResultVo getPassword(String password){
        String passwordEnc = passwordEncoder.encode(password);
        return new ResultVo("",200,passwordEnc);
    }

    @RequestMapping("/getRedisUser")
    public ResultVo getRedisUser(){
        redisService.set("name","张三",60L);
        String name = redisService.get("name");
        List<User> list = userService.list();
        return new ResultVo("成功",200,list);
    }

    @RequestMapping("/saveUser")
    public ResultVo saveUser(){
        List<User> list = userService.list();
        List<User> listView = new ArrayList<>();
        int size = list.size();
        User user = null;
        for(int i = 0;i < size;i++){
            user = list.get(i);
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            listView.add(user);
        }
        return new ResultVo("",200,listView);
    }

    @RequestMapping("/modifyPwd")
    public ResultVo modifyPwd(){
        List<User> list = userService.list();
        List<User> listView = new ArrayList<>();
        int size = list.size();
        User user = null;
        for(int i = 0;i < size;i++){
            user = list.get(i);
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            listView.add(user);
        }
        return new ResultVo("",200,listView);
    }

    @RequestMapping("/removeUser")
    public ResultVo removeUser(){
        List<User> list = userService.list();
        List<User> listView = new ArrayList<>();
        int size = list.size();
        User user = null;
        for(int i = 0;i < size;i++){
            user = list.get(i);
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            listView.add(user);
        }
        return new ResultVo("",200,listView);
    }

}
